// 导入外部模块
const { customRequire } = require('./utils.js');
const { Page, expect } = customRequire('playwright');

class ReportListPage {
    constructor(page) {
        this.page = page;
    }

    async enter_report_list_page() {
        await this.page.getByRole('link', { name: 'Report' }).click();
        await this.page.getByRole('link', { name: 'Order List', exact: true }).click();
    }

    async search_order_no(orderNo) {
        // UAT选择Order List Tab
        await this.page.getByRole('tab', { name: 'Order List', exact: true }).locator('span').click();
        const orderNoXpath = '//*[@id="order-list-tab"]/div[1]/form/div[1]/div[1]/div[1]/div/div/div/div/input';
        await this.page.locator(orderNoXpath).first().click();
        await this.page.locator(orderNoXpath).first().fill(orderNo);
        await this.page.locator('li').filter({ hasText: orderNo }).click();
        // await this.page.getByText(orderNo).click();
        await this.page.getByRole('button', { name: 'Search' }).click();
        await this.page.waitForTimeout(1000);
    }

    async click_first_order_no(orderNo) {
        const firstRowOrderNoXpath = '//*[@id="pane-myReportTab"]/div/div/div[2]/div[4]/div[2]/table/tbody/tr/td[2]/div/a';
        const text = await this.page.locator(firstRowOrderNoXpath).textContent();
        if (text === orderNo) {
            await this.page.getByRole('cell', { name: `${orderNo}` }).locator('a').first().click();
        } else {
            await this.page.locator('button[name="Search"]').click();
        }
    }

    async search_order_no_batch(orderNo) {
        const orderNoString = orderNo.join('\n');
        await this.page.getByRole('textbox', { name: 'Please input Report No.(batch)' }).first().click();
        // await this.page.getByPlaceholder('Please input Order No.(batch)', { exact: true }).first().click();
        // await this.page.getByPlaceholder('Please input Order No.(batch)', { exact: true }).first().fill(orderNoString);
        await this.page.getByRole('textbox', { name: 'Please input Report No.(batch)' }).first().fill(orderNoString);

        await this.page.getByRole('button', { name: 'Search' }).click();
    }

    async search_report_no_batch(reportNo) {
        const reportNoString = reportNo.join('\n');
        // 等待网络状态稳定，确保页面已完全加载
        await this.page.waitForLoadState('networkidle');
        
        try {
            // 使用更长的超时时间和强制点击
            await this.page.getByPlaceholder('Please input Report No.(batch)', { exact: true }).first().click({ force: true, timeout: 30000 });
            await this.page.getByPlaceholder('Please input Report No.(batch)', { exact: true }).first().fill(reportNoString);
            await this.page.getByRole('button', { name: 'Search' }).click({ force: true, timeout: 30000 });
            
            // 等待搜索结果加载
            await this.page.waitForTimeout(2000);
            
            // 修改显示条目100/page - 使用多种选择器策略
            console.log('尝试点击页码选择器...');
            try {
                // 修改显示条目100/page
                const xx = '//*[@id="pane-myReportTab"]/div/div/div[3]/div/span[2]/div/div/input'
                await this.page.locator(xx).click();
                const yy = '//html/body/div[4]/div[1]/div[1]/ul/li[5]/span'
                // //html/body/div[5]/div[1]/div[1]/ul/li[5]/span
                await this.page.locator(yy).click();
                await this.page.waitForTimeout(5000);
            } catch (e) {
                console.log(`设置100/page失败: ${e.message}`);
            }
            
            // 等待页面刷新
            await this.page.waitForTimeout(5000);
        } catch (error) {
            console.error(`搜索报告号批量操作失败: ${error.message}`);
            throw error;
        }
    }

    async search_TRF_no(trfNo) {
        await this.page.getByPlaceholder('Please input TRF No', { exact: true }).first().click();
        await this.page.getByPlaceholder('Please input TRF No', { exact: true }).first().fill(trfNo);
        await this.page.getByText(trfNo).click();
        await this.page.getByRole('button', { name: 'Search' }).click();
    }

    async click_action_button(trNo) {
        const actionButtonXpath = `//*[@id="pane-myReportTab"]/div/div/div[2]/div[5]/div[2]/table/tbody/tr[${trNo}]/td[21]/div/div/button`;
        console.log(`点击第 ${trNo} 行的操作按钮...`);
        try {
            const actionButton = this.page.locator(actionButtonXpath);
            await actionButton.waitFor({state: 'visible', timeout: 30000});
            await actionButton.click({timeout: 30000});
            console.log('action按钮已点击');
            return;
        } catch (error) {
            console.error(`action点击失败: ${error.message}`);
            throw new Error(`无法点击第 ${trNo} 行的操作按钮，请检查元素是否存在或可见`);
        }
    }
}

module.exports = {
    ReportListPage
};
